/**
 * 注册
 */

$(function () {
    layui.use(['form', 'layer'], function () {
        var form = layui.form, layer = layui.layer;

        //错误提示jq object
        const $errorTip = $('.error-tip ');

        /**
         * 登录
         * @param field
         */
        let signup = function(field) {
            //显示loading动画
            // $(CONTENT_LOADING).insertBefore()
            $('#signupButton').addClass('layui-hide');
            $('#signup-loading-icon').removeClass('layui-hide');

            let success = false;
            $.ajax({
                url:'/user/signup',
                type: 'POST',
                data:field,
                dataType: 'json',
                success: (res)=>{
                    if(res.code == "1000"){
                        // $('<label style="display: block;">注册，正在跳转...</label>').insertAfter($('#signup-loading-icon'));
                        success = true;
                        // window.location.href = '/user/index';
                        layer.msg('注册成功');
                        $("#toLogin").removeClass('layui-hide');
                    }else{
                        $errorTip.removeClass('layui-hide').text(res.message);
                    }

                },
                error: (res)=>{
                    layer.msg((res.status || '') + '-出错啦', {icon:2});
                },
                complete: ()=>{
                    $('#signup-loading-icon').addClass('layui-hide');
                    if(success){return;}
                    $('#signupButton').removeClass('layui-hide');
                }
            });
        };


        /**
         * 表单提交
         */
        form.on("submit(signupButton)", function (data) {
            $(data.elem).addClass('layui-disable');
            console.log(data.field);
            signup(data.field);
            return false;
        });

        $('#signupForm input[name]').on('focus', ()=>{
            $errorTip.addClass('layui-hide');
        })

    })
});
